package com.zy.user.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.zy.user.base.BaseDao;
import com.zy.user.base.BaseServiceImpl;
import com.zy.user.converter.UserInfoConverter;
import com.zy.user.dao.UserInfoDao;
import com.zy.user.dto.CommonIdDTO;
import com.zy.user.dto.PageDTO;
import com.zy.user.dto.UserInfoDTO;
import com.zy.user.entity.UserInfo;
import com.zy.user.service.UserInfoService;
import com.zy.user.util.PageHelperTool;
import com.zy.user.vo.UserInfoVO;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * (表t_user_info)服务接口实现
 *
 * @author ZY
 * @date 2021-04-12 18:42:48
 */
//@DubboService(interfaceClass = UserInfoService.class)
@Service("userInfoService")
public class UserInfoServiceImpl extends BaseServiceImpl<UserInfo, Integer, Object> implements UserInfoService {

    /**
     * 注入持久层对象
     */
    @Resource
    private UserInfoDao dao;

    /**
     * 定义抽象方法getDao获取当前实体Mapper对象
     *
     * @return 当前实体Mapper对象
     */
    @Override
    protected BaseDao<UserInfo> getDao() {
        return this.dao;
    }

    /**
     * 新增数据
     *
     * @param dto 参数对象
     * @return 视图对象
     */
    @Override
    public UserInfoVO insert(UserInfoDTO dto) {
        UserInfo entity = UserInfoConverter.convertFromDTO(dto);
        UserInfo res = super.insertSelective(entity);
        if (res != null) {
            return UserInfoConverter.convertToVO(res);
        }
        return null;
    }

    /**
     * 删除对象
     *
     * @param dto 参数对象
     * @return true:删除成功 false:删除失败
     */
    @Override
    public Integer delete(CommonIdDTO dto) {
        return super.deleteByPrimaryKey(Integer.parseInt(dto.getId()));
    }

    /**
     * 更新对象
     *
     * @param dto 待更新的DTO对象
     * @return 成功返回更新后的VO对象，失败返回null
     */
    @Override
    public UserInfoVO update(UserInfoDTO dto) {
        UserInfo entity = UserInfoConverter.convertFromDTO(dto);
        int res = super.updateByPrimaryKeySelective(entity);
        if (res == 1) {
            return UserInfoConverter.convertToVO(entity);
        }
        return null;
    }

    /**
     * 根据ID查询数据
     *
     * @param dto 主键DTO对象
     * @return 成功返回对应ID的VO对象，失败返回null
     */
    @Override
    public UserInfoVO getById(CommonIdDTO dto) {
        UserInfo entity = super.getByPrimaryKey(Integer.parseInt(dto.getId()));
        if (entity != null) {
            return UserInfoConverter.convertToVO(entity);
        }
        return null;
    }

    /**
     * 根据条件查询数据集合
     *
     * @param dto 查询条件对象
     * @return 满足条件的数据集合
     */
    @Override
    public List<UserInfoVO> getList(UserInfoDTO dto) {
        Example example = new Example(UserInfo.class);
        // TODO 根据自身业务情况组装查询条件查询列表(下面是示例)
        //Example.Criteria criteria = example.createCriteria();
        //criteria.andEqualTo("id", dto.getId());
        //criteria.andGreaterThanOrEqualTo("createTime", dto.getCreateTimeBegin());
        //criteria.andLessThanOrEqualTo("createTime", dto.getCreateTimeEnd());
        List<UserInfo> list = super.selectByExample(example);
        if (!CollectionUtils.isEmpty(list)) {
            List<UserInfoVO> retList = new ArrayList<>();
            list.forEach(x -> retList.add(UserInfoConverter.convertToVO(x)));
            return retList;
        }
        return new ArrayList<>();
    }

    /**
     * 根据条件查询分页
     *
     * @param dto  查询条件
     * @param page 分页条件
     * @return 满足条件的分页
     */
    @Override
    public PageInfo<UserInfoVO> getPageInfo(UserInfoDTO dto, PageDTO page) {
        Integer pageNum = page.getPageNum();
        Integer pageSize = page.getPageSize();

        Example example = new Example(UserInfo.class);
        // TODO 根据自身业务情况组装查询条件查询分页(下面是示例)
        //Example.Criteria criteria = example.createCriteria();
        //criteria.andEqualTo("id", dto.getId());
        //criteria.andGreaterThanOrEqualTo("createTime", dto.getCreateTimeBegin());
        //criteria.andLessThanOrEqualTo("createTime", dto.getCreateTimeEnd());
        PageInfo<UserInfo> pageInfo = PageHelper.startPage(pageNum, pageSize).setOrderBy("id desc").doSelectPageInfo(() -> super.selectByExample(example));
        if (pageInfo == null || CollectionUtils.isEmpty(pageInfo.getList())) {
            return PageHelperTool.initPageInfoObj(pageNum, 0, pageSize, new PageInfo<>(Lists.newArrayList()));
        }
        List<UserInfoVO> retList = new ArrayList<>();
        pageInfo.getList().forEach(x -> retList.add(UserInfoConverter.convertToVO(x)));
        return PageHelperTool.initPageInfoObj(pageNum, (int) pageInfo.getTotal(), pageSize, new PageInfo<>(retList));
    }
}